﻿/*
* This software is the confidential and proprietary
* information of NetBay
* Such confidential information should not be disclosed
* NetBay copyright(c) 2010
*/

#region Using
using System.Collections.Generic;
using NetBay.Core.BusinessEntities;
using System;
using NetBay.Core.Log;
using System.Linq;
using Rio.Doc.DataAccess;
using Rio.Doc.Business.Repository.Translator;
#endregion

namespace Rio.Doc.Business.Repository
{
    public class OcrMaskRepository
    {

        /// <summary>
        /// Adds the mask.
        /// </summary>
        /// <param name="p_sMaskLibelle">p_sMaskLibelle</param>
        /// <param name="p_lFolderTypeId">p_lFolderTypeId</param>
        /// <param name="p_sAuthorAreaName">p_sAuthorAreaName</param>
        /// <param name="p_lAuthorAreaTopLeftXCoord">p_lAuthorAreaTopLeftXCoord</param>
        /// <param name="p_lAuthorAreaTopLeftYCoord">p_lAuthorAreaTopLeftYCoord</param>
        /// <param name="p_lAuthorAreaHeight">p_lAuthorAreaHeight</param>
        /// <param name="p_lAuthorAreaWidth">p_lAuthorAreaWidth</param>
        /// <param name="p_lAuthorOcrType">p_lAuthorOcrType</param>
        /// <param name="p_sChronoNumberAreaName">p_sChronoNumberAreaName</param>
        /// <param name="p_lChronoNumberAreaTopLeftXCoord">p_lChronoNumberAreaTopLeftXCoord</param>
        /// <param name="p_lChronoNumberAreaTopLeftYCoord">p_lChronoNumberAreaTopLeftYCoord</param>
        /// <param name="p_lChronoNumberAreaHeight">p_lChronoNumberAreaHeight</param>
        /// <param name="p_lChronoNumberAreaWidth">p_lChronoNumberAreaWidth</param>
        /// <param name="p_lChronoNumberOcrType">p_lChronoNumberOcrType</param>
        /// <returns>Identifier of new mask</returns>
        public static int AddMask(string p_sMaskLibelle, long p_lFolderTypeId, string p_sAuthorAreaName, long p_lAuthorAreaTopLeftXCoord,
            long p_lAuthorAreaTopLeftYCoord, long p_lAuthorAreaHeight, long p_lAuthorAreaWidth, long p_lAuthorOcrType, string p_sChronoNumberAreaName,
            long p_lChronoNumberAreaTopLeftXCoord, long p_lChronoNumberAreaTopLeftYCoord, long p_lChronoNumberAreaHeight, long p_lChronoNumberAreaWidth,
            long p_lChronoNumberOcrType)
        {
            var l_iRequestResult = -1;

            try
            {
                using (var ocrMaskIdDataReader = DA_Ocr.AddMask(p_sMaskLibelle,
                       p_lFolderTypeId,
                       p_sAuthorAreaName,
                       p_lAuthorAreaTopLeftXCoord,
                       p_lAuthorAreaTopLeftYCoord,
                       p_lAuthorAreaHeight,
                       p_lAuthorAreaWidth,
                       p_lAuthorOcrType,
                       p_sChronoNumberAreaName,
                       p_lChronoNumberAreaTopLeftXCoord,
                       p_lChronoNumberAreaTopLeftYCoord,
                       p_lChronoNumberAreaHeight,
                       p_lChronoNumberAreaWidth,
                       p_lChronoNumberOcrType))
                {
                    while (ocrMaskIdDataReader.Read())
                    {
                        l_iRequestResult = Convert.ToInt32(ocrMaskIdDataReader["OcrMaskId"]);
                    }
                }
            }
            catch (Exception e)
            {
                LogManager.Instance().LogError(e.Message);
            }

            return l_iRequestResult;
        }

        /// <summary>
        /// Adds the region.
        /// </summary>
        /// <param name="p_lOcrMaskId">p_lOcrMaskId</param>
        /// <param name="p_lIndexId">p_lIndexId</param>
        /// <param name="p_lRegionTopLeftXCoord">p_lRegionTopLeftXCoord</param>
        /// <param name="p_lRegionTopLeftYCoord">p_lRegionTopLeftYCoord</param>
        /// <param name="p_lRegionHeight">p_lRegionHeight</param>
        /// <param name="p_lRegionWidth">p_lRegionWidth</param>
        /// <returns>Identifier of the new region</returns>
        public static int AddRegion(long p_lOcrMaskId, long p_lIndexId, long p_lRegionTopLeftXCoord, long p_lRegionTopLeftYCoord, long p_lRegionHeight,
            long p_lRegionWidth, string p_sRegionName, long p_lOcrTypeId)
        {
            var l_iRequestResult = -1;

            try
            {
                using (var ocrRegionIdDataReader = DA_Ocr.AddRegion(p_lOcrMaskId, p_lIndexId, p_lRegionTopLeftXCoord,
                                                                      p_lRegionTopLeftYCoord, p_lRegionHeight,
                                                                      p_lRegionWidth, p_sRegionName, p_lOcrTypeId))
                {
                    while (ocrRegionIdDataReader.Read() )
                    {
                        l_iRequestResult = Convert.ToInt32(ocrRegionIdDataReader["OcrRegionId"]);
                    }
                }


            }
            catch (Exception e)
            {
                LogManager.Instance().LogError(e.Message);
            }

            return l_iRequestResult;
        }

        /// <summary>
        /// Gets the mask by folder type id.
        /// </summary>
        /// <param name="p_lFolderTypeId">p_lFolderTypeId</param>
        /// <returns></returns>
        public static List<OcrMask> GetMaskByFolderTypeId(long p_lFolderTypeId)
        {
            return  OcrMaskTranslator.TranslateMask(DA_Ocr.GetMaskByFolderTypeId(p_lFolderTypeId));
        }

        /// <summary>
        /// Gets the mask by folder type id.
        /// </summary>
        /// <param name="p_lFolderTypeId">p_lFolderTypeId</param>
        /// <returns></returns>
        public static OcrMask GetMaskById(long p_lMaskId)
        {
            return OcrMaskTranslator.TranslateMask(DA_Ocr.GetMaskById(p_lMaskId)).First();
        }

        /// <summary>
        /// Gets the region by mask id.
        /// </summary>
        /// <param name="p_lOcrMaskId">p_lOcrMaskId</param>
        /// <returns></returns>
        public static List<OcrMaskItem> GetRegionByMaskId(long p_lOcrMaskId)
        {
            return OcrMaskTranslator.TranslateRegion(DA_Ocr.GetRegionByMaskId(p_lOcrMaskId));
        }

        /// <summary>
        /// Deletes the mask.
        /// </summary>
        /// <param name="p_lOcrMaskId">p_lOcrMaskId</param>
        public static void DeleteMask(long p_lOcrMaskId)
        {
            DA_Ocr.DeleteMask(p_lOcrMaskId);
        }

        /// <summary>
        /// Updates the name of mask and Author and Chrono Number Informations, not Regions !
        /// </summary>
        /// <param name="p_lOcrMaskId">p_lOcrMaskId</param>
        /// <param name="p_sOcrMaskLibelle">p_sOcrMaskLibelle</param>
        public static void UpdateMask(long p_lOcrMaskId, string p_sOcrMaskLibelle, string p_sAuthorAreaName, long p_lAuthorAreaTopLeftXCoord,
            long p_lAuthorAreaTopLeftYCoord, long p_lAuthorAreaHeight, long p_lAuthorAreaWidth, long p_lAuthorOcrType, string p_sChronoNumberAreaName,
            long p_lChronoNumberAreaTopLeftXCoord, long p_lChronoNumberAreaTopLeftYCoord, long p_lChronoNumberAreaHeight, long p_lChronoNumberAreaWidth,
            long p_lChronoNumberOcrType)
        {
            DA_Ocr.UpdateMask(p_lOcrMaskId,
                p_sOcrMaskLibelle,
                p_sAuthorAreaName,
                p_lAuthorAreaTopLeftXCoord,
                p_lAuthorAreaTopLeftYCoord,
                p_lAuthorAreaHeight,
                p_lAuthorAreaWidth,
                p_lAuthorOcrType,
                p_sChronoNumberAreaName,
                p_lChronoNumberAreaTopLeftXCoord,
                p_lChronoNumberAreaTopLeftYCoord,
                p_lChronoNumberAreaHeight,
                p_lChronoNumberAreaWidth,
                p_lChronoNumberOcrType);
        }

        /// <summary>
        /// Updates the region.
        /// </summary>
        /// <param name="p_lOcrRegionId">p_lOcrRegionId</param>
        /// <param name="p_lRegionTopLeftXCoord">p_lRegionTopLeftXCoord</param>
        /// <param name="p_lRegionTopLeftYCoord">p_lRegionTopLeftYCoord</param>
        /// <param name="p_lRegionHeight">p_lRegionHeight</param>
        /// <param name="p_lRegionWidth">p_lRegionWidth</param>
        public static void UpdateRegion(long p_lOcrRegionId, long p_lRegionTopLeftXCoord, long p_lRegionTopLeftYCoord,
            long p_lRegionHeight, long p_lRegionWidth, long p_lOcrTypeId, string p_sOcrRegionName)
        {
            DA_Ocr.UpdateRegion(p_lOcrRegionId, p_lRegionTopLeftXCoord, p_lRegionTopLeftYCoord, p_lRegionHeight, p_lRegionWidth, p_lOcrTypeId, p_sOcrRegionName);
        }
    }
}
